Skip to content

fix: support negative array index for set/delete/map (#1077) - #1079

Open
arimu1 wants to merge 1 commit into
json-path:masterfrom
arimu1:fix/1077-negative-array-index-mutate
Open

fix: support negative array index for set/delete/map (#1077)#1079
arimu1 wants to merge 1 commit into
json-path:masterfrom
arimu1:fix/1077-negative-array-index-mutate

Conversation

@arimu1

@arimu1 arimu1 commented Aug 5, 2026

Copy link
Copy Markdown

Fixes

Fixes #1077

What

JsonPath.read already resolves negative array indices (e.g. $[-1] → last element). set, delete, and map used the raw negative index when building the update PathRef, so the JSON provider threw an uncaught IndexOutOfBoundsException.

Normalize the index (length + index when negative) before creating the update PathRef, matching the read path.

Why

Read and mutate should agree on $[-n]. Callers should not get a raw JDK exception for a path that read already supports.

Changes

  • PathToken.handleArrayIndex: compute effectiveIndex first, then PathRef.create(model, effectiveIndex)
  • WriteTest: set / delete / map on $[-1]

Test plan

  • ./gradlew :json-path:test --tests com.jayway.jsonpath.WriteTest — 35/35 pass
  • ./gradlew :json-path:test — full module suite pass
  • Platform: macOS aarch64, Temurin 21.0.11

Normalize negative array indices before creating the update PathRef so
set, delete, and map resolve $[-n] the same way as read.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Negative array index on set/delete/map throws a raw IndexOutOfBoundsException while read supports it

1 participant